<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisble" :before-close="handleClose" :close-on-click-modal=false
               width="700px">
      <el-form :model="form" label-position="left" label-width="120px" ref="form" :rules="rules">
        <el-form-item label="应用类型" prop="appType">
          <el-radio-group v-model="form.appType">
            <el-radio :label=1>企业钉钉</el-radio>
            <el-radio :label=2>企业微信</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="接收群组" prop="dingTalkName">
          <el-input v-model="form.dingTalkName" autocomplete="off" maxlength="200"></el-input>
        </el-form-item>
        <el-form-item label="Webhook地址" prop="dingTalkToken">
          <el-input v-model="form.dingTalkToken" autocomplete="off" maxlength="100" type="password"></el-input>
        </el-form-item>
        <el-form-item label="加签秘钥" prop="dingTalkSecret">
          <el-input v-model="form.dingTalkSecret" autocomplete="off" maxlength="100" type="password"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
export default {
  name: "cuDialog",
  props: ["dialogVisble", "title", "editData"],
  data() {
    return {
      form: {
        "appType":"",
        "dingTalkName": "",
        "dingTalkToken": "",
        "dingTalkSecret": ""
      },
      rules: {
        appType: [{required: true, message: '应用类型不能为空', trigger: 'change'}],
        dingTalkName: [{required: true, message: '接收群组不能为空', trigger: 'blur'}],
        dingTalkToken: [{required: true, message: 'Webhook地址不能为空', trigger: 'blur'}],
        // dingTalkSecret: [{required: true, message: '加签秘钥不能为空', trigger: 'blur'}],
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit("closeDialog")
    },
    save() {
      console.log("form", this.form)
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.title === "新建配置") {
            this.form.createUser = this.$store.state.user.userId
            this.api({url: "/addDingTalkConfig", method: "post", data: this.form}).then(resp => {
              this.$emit("refreshList")
              this.$message({message: resp, type: 'success'});
            })
          } else {
            this.form.updateUser = this.$store.state.user.userId
            this.api({url: "/updateDingTalkConfig", method: "put", data: this.form}).then(resp => {
              this.$emit("refreshList")
              this.$message({message: resp, type: 'success'});
            })
          }
        }
      })
    }
  },
  watch: {
    editData() {
      this.form = {...this.editData}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    }
  }
}
</script>

<style scoped>

</style>
